class Employee < ActiveRecord::Base
  
  validates :full_name, :mobile, :presence => true
  validates :full_name, :uniqueness => true
  has_many :posts, :dependent => :destroy

  scope :installers, order('full_name asc')
  scope :visible, where('status = ?','visible')
  
  def self.current
    find(Thread.current[:current_employee_id])
  end
  
  def visible!
    update_attribute :status, 'visible'
  end

  def hidden!
    update_attribute :status, 'hidden'
  end

  def status
    ActiveSupport::StringInquirer.new(self[:status])
  end
  
  def reset_posts!(saler_in_dept_ids, officer_in_dept_ids,installer_in_dept_ids,store_admin_in_dept_ids)
    self.posts = []
    saler_in_dept_ids.each do |dept_id|
      self.posts << Post.new( 
        :dept_id => dept_id,
        :name => 'saler',
        :employee_id => id
      )
    end if saler_in_dept_ids.present?
    officer_in_dept_ids.each do |dept_id|
      self.posts << Post.new(
        :dept_id => dept_id,
        :name => 'officer',
        :employee_id => id
      ) 
    end if officer_in_dept_ids.present?
    installer_in_dept_ids.each do |dept_id|
      self.posts << Post.new( 
        :dept_id => dept_id,
        :name => 'installer',
        :employee_id => id
      )
    end if installer_in_dept_ids.present?
    store_admin_in_dept_ids.each do |dept_id|
      self.posts << Post.new( 
        :dept_id => dept_id,
        :name => 'store_admin',
        :employee_id => id
      )
    end if store_admin_in_dept_ids.present?
  end
  
  def is_officer?(args = {})
    Post.where(args.merge(:employee_id => id, :name => 'officer')).present?
  end
  
  def is_saler?(args = {})
    Post.where(args.merge(:employee_id => id, :name => 'saler')).present?
  end
  
  def is_store_admin?(args = {})
    Post.where(args.merge(:employee_id => id, :name => 'store_admin')).present?
  end
  
  def is_installer?(args = {})
    Post.where(args.merge(:employee_id => id, :name => 'installer')).present?
  end
  
  def posts_by_dept_id(dept_id)
    Post.where(:dept_id => dept_id, :employee_id => id).order('created_at asc')
  end

end
